﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.SOESupport;

namespace WorldViewSolutions.ArcGISServer.SOE
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks></remarks>
	public static class JsonHelper
	{
        /// <summary>
        /// Parses the boolean.
        /// </summary>
        /// <param name="json">The json.</param>
        /// <param name="property">The property.</param>
        /// <returns></returns>
        /// <remarks></remarks>
		public static bool ParseBoolean(JsonObject json, string property)
		{
			bool? val;
			if (!json.TryGetAsBoolean(property, out val))
				throw new Exception("Parameter '" + property + "' is missing or the value is not a valid.");

			return val.Value;
		}

        /// <summary>
        /// Parses the double.
        /// </summary>
        /// <param name="json">The json.</param>
        /// <param name="property">The property.</param>
        /// <returns></returns>
        /// <remarks></remarks>
		public static double ParseDouble(JsonObject json, string property)
		{
			double? val;
			if (!json.TryGetAsDouble(property, out val))
				throw new Exception("Parameter '" + property + "' is missing or the value is not a valid.");

			return val.Value;
		}

        /// <summary>
        /// Parses the double.
        /// </summary>
        /// <param name="json">The json.</param>
        /// <param name="property">The property.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static double ParseDouble(JsonObject json, string property, double defaultValue)
        {
            double? val;
            if (!json.TryGetAsDouble(property, out val))
                return defaultValue;

            return val.Value;
        }

        /// <summary>
        /// Parses the int.
        /// </summary>
        /// <param name="json">The json.</param>
        /// <param name="property">The property.</param>
        /// <returns></returns>
        /// <remarks></remarks>
		public static int ParseInt(JsonObject json, string property)
		{
			long? val;
			if (!json.TryGetAsLong(property, out val))
				throw new Exception("Parameter '" + property + "' is missing or the value is not a valid.");

			return Convert.ToInt32(val.Value);
		}

        /// <summary>
        /// Parses the int.
        /// </summary>
        /// <param name="json">The json.</param>
        /// <param name="property">The property.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static int ParseInt(JsonObject json, string property, int defaultValue)
        {
            long? val;
            if (!json.TryGetAsLong(property, out val))
                return defaultValue;

            return Convert.ToInt32(val.Value);
        }

        /// <summary>
        /// Parses the string.
        /// </summary>
        /// <param name="json">The json.</param>
        /// <param name="property">The property.</param>
        /// <returns></returns>
        /// <remarks></remarks>
		public static string ParseString(JsonObject json, string property)
		{
			string val;
			if (!json.TryGetString(property, out val))
				throw new Exception("Parameter '" + property + "' is missing or the value is not a valid.");

			return val;
		}
	}
}
